package com.feiwo.nonego.rpc.common.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Optional;

/**
 *
 *
 * @author feiwo 2025-08-19
 */
public class IpUtils {

    private static final Logger logger = LoggerFactory.getLogger(IpUtils.class);

    public static Optional<InetAddress> getLocalInetAddress() {
        try {
            return Optional.of(InetAddress.getLocalHost());
        } catch (UnknownHostException e) {
            logger.error("get local ip address throws ex ---> ", e);
        }
        return Optional.empty();
    }

    public static String getLocalAddress() {
        return getLocalInetAddress().map(InetAddress::toString).orElse("");
    }

    public static String getHostName() {
        return getLocalInetAddress().map(InetAddress::getHostName).orElse("");
    }

    public static String getLocalHostIp() {
        return getLocalInetAddress().map(InetAddress::getHostAddress).orElse("");
    }

}
